﻿Public Class Evento

    Public PK_Evento As Integer

    Public Property NomeEvento As String

    Public Property Descrizione As String

    Public Property DataInizio As Date

    Public Property DataFine As Date

    Public Property Link As String

    Public Property Deleted As Boolean

    Public Property Priorita As Integer

    Public Property Prezzo As String

    Public Property DataModifica As Date

    Public Property ImgEvento As String

    Public Property brochure As String

    Public Property IngressoLibero As Boolean

    Public FK_Target() As Target

    Public FK_Indirizzo As Indirizzo

    Public FK_Organizzatore As Organizzatore

    Public FK_Iscrizione As Iscrizione

    Public Enum Categoria
        concerto = 1
        festa
        discoteca
        fiera
        sagra
        gita
        gratis
        mostra
        generico = 100
    End Enum

    Public intCategoria As Integer

    Public Property enCategoria As Categoria
        Set(value As Categoria)
            intCategoria = CInt(value)
        End Set
        Get
            Return intCategoria
        End Get
    End Property


    Public Sub LoadFromPk(objpk As Integer)

        ' Dim Evt As New Evento
        Dim sql As String = "SELECT E.*, I.PK_Indirizzo " & _
                            " FROM Evento E LEFT OUTER JOIN Indirizzo I ON E.FK_Indirizzo = I.PK_Indirizzo WHERE E.Deleted = 0 AND PK_Evento = " & objpk
        Dim ds As New DataSet
        Dim dbO As New DBObject
        'Throw New Exception(sql)
        ds = dbO.FillDS(sql, Nothing, 1)
        If Not IsNothing(ds) AndAlso Not IsNothing(ds.Tables) AndAlso ds.Tables.Count > 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
            PK_Evento = objpk
            If Not IsDBNull(ds.Tables(0).Rows(0).Item("NomeEvento")) Then
                NomeEvento = ds.Tables(0).Rows(0).Item("NomeEvento").ToString
            End If
            If Not IsDBNull(ds.Tables(0).Rows(0).Item("Descrizione")) Then
                Descrizione = ds.Tables(0).Rows(0).Item("Descrizione").ToString
            End If
            If Not IsDBNull(ds.Tables(0).Rows(0).Item("DataInizio")) Then
                DataInizio = ds.Tables(0).Rows(0).Item("DataInizio")
            End If
            If Not IsDBNull(ds.Tables(0).Rows(0).Item("DataFine")) Then
                DataFine = ds.Tables(0).Rows(0).Item("DataFine")
            End If

            If Not IsDBNull(ds.Tables(0).Rows(0).Item("intCategoria")) Then
                enCategoria = ds.Tables(0).Rows(0).Item("intCategoria")
            End If

            If Not IsDBNull(ds.Tables(0).Rows(0).Item("Link")) Then
                Link = ds.Tables(0).Rows(0).Item("Link")
            End If

            If Not IsDBNull(ds.Tables(0).Rows(0).Item("IngressoLibero")) Then
                IngressoLibero = CBool(ds.Tables(0).Rows(0).Item("IngressoLibero"))
            End If

            FK_Indirizzo = New Indirizzo
            FK_Indirizzo.LoadFromPk(ds.Tables(0).Rows(0).Item("FK_Indirizzo"))

            If Not IsDBNull(ds.Tables(0).Rows(0).Item("FK_Iscrizione")) Then
                FK_Iscrizione = New Iscrizione
                FK_Iscrizione.LoadFromPk(ds.Tables(0).Rows(0).Item("FK_Iscrizione"))
            End If

            If Not IsDBNull(ds.Tables(0).Rows(0).Item("Priorita")) Then
                Priorita = ds.Tables(0).Rows(0).Item("Priorita")
            End If

            If Not IsDBNull(ds.Tables(0).Rows(0).Item("ImgEvento")) Then
                ImgEvento = ds.Tables(0).Rows(0).Item("ImgEvento")
            End If

            If Not IsDBNull(ds.Tables(0).Rows(0).Item("Prezzo")) Then
                Prezzo = ds.Tables(0).Rows(0).Item("Prezzo")
            End If

            If Not IsDBNull(ds.Tables(0).Rows(0).Item("DataModifica")) Then
                DataModifica = ds.Tables(0).Rows(0).Item("DataModifica")
            End If

            If Not IsDBNull(ds.Tables(0).Rows(0).Item("Brochure")) Then
                brochure = ds.Tables(0).Rows(0).Item("Brochure")
            End If

            sql = "SELECT * FROM Target INNER JOIN Evento_Target on PK_Target = FK_Target WHERE FK_Evento = " & objpk
            ds = New DataSet
            dbO = New DBObject
            ds = dbO.FillDS(sql, Nothing, 1)
            If Not IsNothing(ds) AndAlso Not IsNothing(ds.Tables) AndAlso ds.Tables.Count > 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                Dim Trg As Target
                Dim targetList As New List(Of Target)
                For Each r As DataRow In ds.Tables(0).Rows
                    Trg = New Target
                    Trg = Target.LoadFromPk(ds.Tables(0).Rows(0).Item("FK_Target"))
                    targetList.Add(Trg)
                Next
                FK_Target = targetList.ToArray
            End If

            sql = "SELECT * FROM Organizzatore INNER JOIN Evento_Organizzatore on PK_Organizzatore = FK_organizzatore WHERE FK_Evento = " & objpk
            ds = New DataSet
            dbO = New DBObject
            ds = dbO.FillDS(sql, Nothing, 1)
            If Not IsNothing(ds) AndAlso Not IsNothing(ds.Tables) AndAlso ds.Tables.Count > 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                Dim Org As New Organizzatore
                Dim orgList As New List(Of Organizzatore)

                Org = Organizzatore.LoadFromPk(ds.Tables(0).Rows(0).Item("FK_Organizzatore"))

                FK_Organizzatore = Org
            End If

        End If

    End Sub

    Public Shared Function CancellaEvento(PK_evento As Integer) As Boolean

        If PK_evento = 0 Then
            Return False
        Else
            Dim dbo As New DBObject
            Dim sql As String = "UPDATE Evento SET " & _
                                " Deleted= '" & True & "'" & _
                               " WHERE PK_Evento = " & PK_evento

            If dbo.ExecuteNonQuery(sql, Nothing, CommandType.Text) > 0 Then

                Return True
            Else
                Return False
            End If

        End If

    End Function


    Public Function Salva() As Boolean

        If PK_Evento = 0 Then
            Return False
        Else
            Dim dbo As New DBObject
            Dim Inizio As String
            Dim Fine As String

            If DataInizio.Equals(Date.MinValue) Then
                Inizio = ""
            Else
                Inizio = DataInizio.ToString("yyyyMMdd")
            End If

            If DataFine.Equals(Date.MinValue) Then
                Fine = ""
            Else
                Fine = DataFine.ToString("yyyyMMdd")
            End If

            FK_Indirizzo.PK_Indirizzo = FK_Indirizzo.Salva()
            FK_Iscrizione.PK_Iscrizione = FK_Iscrizione.Salva()

            Dim sql As String = "UPDATE Evento_Organizzatore set FK_Organizzatore = " & FK_Organizzatore.PK_Organizzatore & _
                                " WHERE FK_Evento = " & PK_Evento

            dbo.ExecuteNonQuery(sql, Nothing, CommandType.Text)

            sql = "UPDATE Evento SET " & _
                                " NomeEvento= '" & NomeEvento.Replace("'", "''") & "'" & _
                                " ,Descrizione= '" & Descrizione.Replace("'", "''") & "'" & _
                                " ,DataInizio= '" & Inizio & "'" & _
                                " ,DataFine= '" & Fine & "'" & _
                                " ,Link= '" & Link & "'" & _
                                " ,IngressoLibero= '" & IngressoLibero & "'" & _
                                " ,Priorita= '" & Priorita & "'" & _
                                " ,FK_Indirizzo= " & FK_Indirizzo.PK_Indirizzo & _
                                " ,FK_Iscrizione= " & FK_Iscrizione.PK_Iscrizione & _
                                " ,ImgEvento= '" & ImgEvento & "'" & _
                                " ,Prezzo= '" & Prezzo.Replace("'", "''") & "'" & _
                                " ,DataModifica = '" & DataModifica.Date.ToString("yyyyMMdd") & "'" & _
                                " ,Brochure= '" & brochure & "'" & _
                                " ,intCategoria = " & intCategoria & _
                                " WHERE PK_Evento = " & PK_Evento

            If dbo.ExecuteNonQuery(sql, Nothing, CommandType.Text) > 0 Then

                ' Throw New Exception(sql)
                Return True
            Else
                Return False
            End If

        End If

    End Function


End Class

Public Class Iscrizione
    Public Property PK_Iscrizione As Integer

    Public Property DataApertura As Date

    Public Property DataChiusura As Date

    Public Property Dettagli As String

    Public Property ModuloIscrizione As String

    Public Property Deleted As Boolean

    Public Sub LoadFromPk(objpk As Integer)

        Dim sql As String = "SELECT * " & _
                            " FROM Iscrizione WHERE PK_Iscrizione = " & objpk

        Dim ds As New DataSet
        Dim dbO As New DBObject
        ds = dbO.FillDS(sql, Nothing, 1)
        If Not IsNothing(ds) AndAlso Not IsNothing(ds.Tables) AndAlso ds.Tables.Count > 0 AndAlso Not IsNothing(ds.Tables(0).Rows) AndAlso ds.Tables(0).Rows.Count > 0 Then
            PK_Iscrizione = ds.Tables(0).Rows(0).Item("PK_Iscrizione")
            If Not IsDBNull(ds.Tables(0).Rows(0).Item("DataApertura")) Then
                DataApertura = ds.Tables(0).Rows(0).Item("DataApertura")
            End If
            If Not IsDBNull(ds.Tables(0).Rows(0).Item("DataChiusura")) Then
                DataChiusura = ds.Tables(0).Rows(0).Item("DataChiusura")
            End If
            If Not IsDBNull(ds.Tables(0).Rows(0).Item("ModuloIscrizione")) Then
                ModuloIscrizione = ds.Tables(0).Rows(0).Item("ModuloIscrizione")
            End If
            If Not IsDBNull(ds.Tables(0).Rows(0).Item("Dettagli")) Then
                Dettagli = ds.Tables(0).Rows(0).Item("Dettagli")
            End If
        End If

    End Sub

    Public Function Salva() As Integer

        Dim Inizio As String
        Dim Fine As String

        If DataApertura.Equals(Date.MinValue) Then
            Inizio = ""
        Else
            Inizio = DataApertura.ToString("yyyyMMdd")
        End If

        If DataChiusura.Equals(Date.MinValue) Then
            Fine = ""
        Else
            Fine = DataChiusura.ToString("yyyyMMdd")
        End If

        If PK_Iscrizione = 0 Then
            Dim dbo As New DBObject
            Dim sql As String = "INSERT INTO Iscrizione (DataApertura, DataChiusura, Dettagli, ModuloIscrizione, Deleted) VALUES( " & _
                                " '" & Inizio & "'," & _
                                " '" & Fine & "'," & _
                                " '" & Dettagli.Replace("'", "''") & "'," & _
                                " '" & ModuloIscrizione & "'," & _
                                " 0)"
            If dbo.ExecuteNonQuery(sql, Nothing, CommandType.Text) > 0 Then
                sql = "SELECT MAX (PK_Iscrizione) FROM Iscrizione"
                dbo.ExecuteScalar(sql, Nothing, CommandType.Text)
                Return dbo.ExecuteScalar(sql, Nothing, CommandType.Text)
            Else
                Return False
            End If
        Else
            Dim dbo As New DBObject
            Dim sql As String = "UPDATE Iscrizione SET " & _
                                " DataApertura= '" & Inizio & "'" & _
                                " ,DataChiusura= '" & Fine & "'" & _
                                " ,Dettagli= '" & Dettagli.Replace("'", "''") & "'" & _
                                " ,ModuloIscrizione= '" & ModuloIscrizione & "'" & _
                                " WHERE PK_Iscrizione = " & PK_Iscrizione

            If dbo.ExecuteNonQuery(sql, Nothing, CommandType.Text) > 0 Then

                Return PK_Iscrizione
            Else
                Return False
            End If

        End If
    End Function

End Class

Public Class EventoPeriodico


    Public PK_EventoPeriodico As Integer

    Public Property Nome As String

    Public Property Descrizione As String

    Public Property Giorno As Integer

    Public Property Deleted As Boolean

    Public Property ImgEvento As String

   
    Public FK_Indirizzo As Indirizzo


    Public Sub LoadFromPk(objpk As Integer)

        ' Dim Evt As New Evento
        Dim sql As String = "SELECT EP.*, I.PK_Indirizzo " & _
                            " FROM EventoPeriodico EP LEFT OUTER JOIN Indirizzo I ON EP.FK_Indirizzo = I.PK_Indirizzo WHERE EP.Deleted = 0 AND EP.PK_EventoPeriodico = " & objpk
        Dim ds As New DataSet
        Dim dbO As New DBObject
        'Throw New Exception(sql)
        ds = dbO.FillDS(sql, Nothing, 1)
        If Not IsNothing(ds) AndAlso Not IsNothing(ds.Tables) AndAlso ds.Tables.Count > 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
            PK_EventoPeriodico = objpk
            If Not IsDBNull(ds.Tables(0).Rows(0).Item("Nome")) Then
                Nome = ds.Tables(0).Rows(0).Item("Nome").ToString
            End If
            If Not IsDBNull(ds.Tables(0).Rows(0).Item("Descrizione")) Then
                Descrizione = ds.Tables(0).Rows(0).Item("Descrizione").ToString
            End If
            If Not IsDBNull(ds.Tables(0).Rows(0).Item("Giorno")) Then
                Giorno = ds.Tables(0).Rows(0).Item("Giorno")
            End If
          

            FK_Indirizzo = New Indirizzo
            FK_Indirizzo.LoadFromPk(ds.Tables(0).Rows(0).Item("FK_Indirizzo"))



            If Not IsDBNull(ds.Tables(0).Rows(0).Item("ImgEvento")) Then
                ImgEvento = ds.Tables(0).Rows(0).Item("ImgEvento")
            End If

         
        End If

    End Sub

    Public Shared Function CancellaEvento(PK_evento As Integer) As Boolean

        If PK_evento = 0 Then
            Return False
        Else
            Dim dbo As New DBObject
            Dim sql As String = "UPDATE EventoPeriodico SET " & _
                                " Deleted= '" & True & "'" & _
                               " WHERE PK_EventoPeriodico = " & PK_evento

            If dbo.ExecuteNonQuery(sql, Nothing, CommandType.Text) > 0 Then

                Return True
            Else
                Return False
            End If

        End If

    End Function


    Public Function Salva() As Boolean

        If PK_EventoPeriodico = 0 Then
            Return False
        Else
            Dim dbo As New DBObject
          

            FK_Indirizzo.PK_Indirizzo = FK_Indirizzo.Salva()
      
            Dim sql As String = ""
            sql = "UPDATE EventoPeriodico SET " & _
                                " Nome= '" & Nome.Replace("'", "''") & "'" & _
                                " ,Descrizione= '" & Descrizione.Replace("'", "''") & "'" & _
                                " ,giorno= '" & Giorno & "'" & _
                                " ,FK_Indirizzo= " & FK_Indirizzo.PK_Indirizzo & _
                                " ,ImgEvento= '" & ImgEvento & "'" & _
                                 " WHERE PK_EventoPeriodico = " & PK_EventoPeriodico

            If dbo.ExecuteNonQuery(sql, Nothing, CommandType.Text) > 0 Then

                ' Throw New Exception(sql)
                Return True
            Else
                Return False
            End If

        End If

    End Function



End Class
